package com.useeinfo.oa.modules.inspection.biz;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.useeinfo.framework.extend.biz.CrudBiz;
import com.useeinfo.framework.sugar.func.lang.StringUtils;
import com.useeinfo.framework.sugar.tools.StringConverters;
import com.useeinfo.oa.common.utils.ResultUtil.ResultDto;
import com.useeinfo.oa.common.utils.ResultUtil.ResultUtil;
import com.useeinfo.oa.modules.base.biz.FacilityBiz;
import com.useeinfo.oa.modules.base.biz.FacilityCatBiz;
import com.useeinfo.oa.modules.base.entity.Facility;
import com.useeinfo.oa.modules.base.entity.FacilityCat;
import com.useeinfo.oa.modules.inspection.dao.FacilityInspectionSnapDao;
import com.useeinfo.oa.modules.inspection.entity.FacilityInspectionSnap;
import com.useeinfo.oa.modules.inspection.entity.InspectionOrder;
import com.useeinfo.oa.modules.inspection.entity.InspectionOrderWorkContent;
import com.useeinfo.oa.modules.inspection.entity.InspectionTechnology;
import com.useeinfo.oa.modules.inspection.enums.InputTypeEnum;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.math.BigInteger;
import java.util.Date;
import java.util.List;

@Service("facilityInspectionSnapBiz")
public class FacilityInspectionSnapBiz extends CrudBiz<FacilityInspectionSnapDao, FacilityInspectionSnap> {
    @Autowired
    private InspectionOrderBiz inspectionOrderBiz;
    @Autowired
    private FacilityBiz facilityBiz;
    @Autowired
    private FacilityCatBiz facilityCatBiz;
    @Autowired
    private InspectionTechnologyBiz inspectionTechnologyBiz;
    @Autowired
    private InspectionOrderWorkContentBiz inspectionOrderWorkContentBiz;

    /**
     * 保存设备数据
     * @param facilityIds
     * @param serial
     * @param request
     * @return
     */
    public ResultDto saveFacility(String facilityIds, String inspectionOrderId, String serial, HttpServletRequest request){

        InspectionOrder inspectionOrder = inspectionOrderBiz.findModel(StringConverters.null2Long(inspectionOrderId));
        net.sf.json.JSONArray jsonArray = net.sf.json.JSONArray.fromObject(facilityIds);
        for (Object id : jsonArray) {
            FacilityInspectionSnap facilityInspectionSnap;
            Facility facility = facilityBiz.findModel(StringConverters.null2Long(id));
            List<FacilityInspectionSnap> facilityInspectionSnapList = dao.findByFacilityId(StringConverters.null2Long(id), serial);
            if (CollectionUtils.isNotEmpty(facilityInspectionSnapList)) {
                facilityInspectionSnap = facilityInspectionSnapList.get(0);
            } else {
                facilityInspectionSnap = new FacilityInspectionSnap();
                facilityInspectionSnap.setTempVal(Boolean.TRUE);
            }
            facilityInspectionSnap.setInspectionOrder(inspectionOrder);
            facilityInspectionSnap.setSerial(serial);
            facilityInspectionSnap.setSpec(facility.getType());
            facilityInspectionSnap.setFacilityId(StringConverters.null2Long(id));
            facilityInspectionSnap.setFacilityName(facility.getName());
            facilityInspectionSnap.setFacilityCode(facility.getCode());
            facilityInspectionSnap.setFacilityGroupId(facility.getFacilityGroup() != null ? facility.getFacilityGroup().getId() : null);
            facilityInspectionSnap.setFacilityGroupName(facility.getFacilityGroup() != null ? facility.getFacilityGroup().getFacilityGroupName() : "");
            facilityInspectionSnap.setAreaId(facility.getArea() != null ? facility.getArea().getId() : null);
            facilityInspectionSnap.setAreaName(facility.getArea() != null ? facility.getArea().getAreaName() : "");
            facilityInspectionSnap.setFacilityCatId(facility.getFacilityCat() != null ? facility.getFacilityCat().getId() : null);
            facilityInspectionSnap.setFacilityCatName(facility.getFacilityCat() != null ? facility.getFacilityCat().getCatName() : "");
            facilityInspectionSnap.setRootCatId(facility.getRootCatId());
            if(facilityInspectionSnap.getId()!=null){
                dao.add(facilityInspectionSnap);
            }else{
                dao.update(facilityInspectionSnap);
            }
        }
        return ResultUtil.success();
    }

    /**
     * 保存工艺数据
     * @param facilityInspectionSnapIds
     * @param inspectionId
     * @param inspectionOrderId
     * @return
     */
    public ResultDto saveInspection(String facilityInspectionSnapIds, String inspectionId, String inspectionOrderId){
        InspectionOrder inspectionOrder = inspectionOrderBiz.findModel(StringConverters.null2Long(inspectionOrderId));
        InspectionTechnology inspectionTechnology = inspectionTechnologyBiz.findModel(StringConverters.null2Long(inspectionId));
        for(String id:facilityInspectionSnapIds.split(",")){
            FacilityInspectionSnap facilityInspectionSnap = dao.findModel(StringConverters.null2Long(id));
            facilityInspectionSnap.setInspectionOrder(inspectionOrder);
            facilityInspectionSnap.setInspectionId(inspectionTechnology.getId());
            facilityInspectionSnap.setInspectionName(inspectionTechnology.getName());
            dao.update(facilityInspectionSnap);
        }
        return ResultUtil.success();

    }

    /**
     * app接口保存设备工艺数据
     * @param inspectionOrderId
     * @param facilityId
     * @param inspectionId
     * @param serial
     */
    public FacilityInspectionSnap saveFacilityInspectionSnap(String inspectionOrderId, String facilityId, String inspectionId, String serial){

        InspectionOrder inspectionOrder = inspectionOrderBiz.findModel(StringConverters.null2Long(inspectionOrderId));
        FacilityInspectionSnap facilityInspectionSnap=new FacilityInspectionSnap();
        Facility facility = facilityBiz.findModel(StringConverters.null2Long(facilityId));
        if(facility==null){
            return null;
        }
        facilityInspectionSnap.setSerial(serial);
        facilityInspectionSnap.setInspectionOrder(inspectionOrder);
        facilityInspectionSnap.setSerial(inspectionOrder.getSerial());
        facilityInspectionSnap.setFacilityId(StringConverters.null2Long(facilityId));
        facilityInspectionSnap.setFacilityName(facility.getName());
        facilityInspectionSnap.setFacilityCode(facility.getCode());
        facilityInspectionSnap.setFacilityGroupId(facility.getFacilityGroup() != null ? facility.getFacilityGroup().getId() : null);
        facilityInspectionSnap.setFacilityGroupName(facility.getFacilityGroup() != null ? facility.getFacilityGroup().getFacilityGroupName() : "");
        facilityInspectionSnap.setAreaId(facility.getArea() != null ? facility.getArea().getId() : null);
        facilityInspectionSnap.setAreaName(facility.getArea() != null ? facility.getArea().getAreaName() : "");
        facilityInspectionSnap.setFacilityCatId(facility.getFacilityCat() != null ? facility.getFacilityCat().getId() : null);
        facilityInspectionSnap.setFacilityCatName(facility.getFacilityCat() != null ? facility.getFacilityCat().getCatName() : "");
        facilityInspectionSnap.setRootCatId(facility.getRootCatId());
        facilityInspectionSnap.setSpec(facility.getType());
        //保存工艺数据
        InspectionTechnology inspectionTechnology = inspectionTechnologyBiz.findModel(StringConverters.null2Long(inspectionId));
        if(inspectionTechnology==null){
            return null;
        }
        facilityInspectionSnap.setInspectionId(inspectionTechnology.getId());
        facilityInspectionSnap.setInspectionName(inspectionTechnology.getName());
        dao.add(facilityInspectionSnap);


        /**解析工艺内容start***/
        JSONArray jsonArray = JSONArray.parseArray(inspectionTechnology.getInspectionDataContent());
        for (Object object : jsonArray) {
            JSONObject jsonObject = (JSONObject) object;
            InspectionOrderWorkContent inspectionOrderWorkContent = new InspectionOrderWorkContent();
            inspectionOrderWorkContent.setCreateDate(new Date());
            inspectionOrderWorkContent.setFacilityInspectionSnap(facilityInspectionSnap);
            inspectionOrderWorkContent.setPhoto(jsonObject.getBoolean("photo"));
            inspectionOrderWorkContent.setName(jsonObject.getString("name"));
            inspectionOrderWorkContent.setMaxNumber(jsonObject.getDouble("maxNumber"));
            inspectionOrderWorkContent.setMinNumber(jsonObject.getDouble("minNumber"));
            inspectionOrderWorkContent.setApplication_scenarios_id(jsonObject.getLong("applicationScenariosId"));
            inspectionOrderWorkContent.setApplication_scenarios_name(jsonObject.getString("applicationScenariosName"));
            inspectionOrderWorkContent.setContent(jsonObject.getString("content"));
            inspectionOrderWorkContent.setResourceIds(jsonObject.getString("resourceIds"));
            inspectionOrderWorkContent.setUnit(jsonObject.getString("unit"));
            inspectionOrderWorkContent.setCategory(jsonObject.getString("category"));
            inspectionOrderWorkContent.setInputTypeEnum(InputTypeEnum.valueOf(jsonObject.getString("inputTypeEnumCode")));
            inspectionOrderWorkContentBiz.addOrUpdate(inspectionOrderWorkContent);
        }
        /**解析工艺内容end***/

        return facilityInspectionSnap;

    }


    /**
     * 作业人员(专业)工作完成情况统计
     *
     * @param enterpriseIds
     * @return
     */
    public JSONArray completePercent(String enterpriseIds,String startDate,String endDate) {
        JSONArray rows = new JSONArray();
        if(StringUtils.isBlank(enterpriseIds)){
            return rows;
        }
        List<Object[]> objects = dao.completePercent(enterpriseIds,startDate,endDate,false,"");
        for(Object obj[] : objects){
            JSONObject json = new JSONObject();
            Long rootCatId = obj[0]==null?null:((BigInteger) obj[0]).longValue();
            Integer totalCount = ((BigInteger) obj[1]).intValue();
            if(rootCatId!=null){
                FacilityCat rootCat = facilityCatBiz.findModel(rootCatId);
                json.put("rootCatName",rootCat.getCatName());
            }else{
                json.put("rootCatName","未分配");
            }
            json.put("rootCatId",rootCatId);
            json.put("totalCount",totalCount);
            //查询已完成的数量
            List<Object[]> objects1 = dao.completePercent(enterpriseIds,startDate,endDate,true,rootCatId+"");
            Integer finishCount = ((BigInteger) objects1.get(0)[1]).intValue();
            json.put("finishCount",finishCount);
            json.put("lastCount",totalCount-finishCount);
            rows.add(json);
        }
        return rows;
    }
}
